package com.lancoo.znbkxx.teacher.net;

import android.content.Context;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.lancoo.cpbase.authentication.base.LoginOperate;

import io.reactivex.ObservableTransformer;
import io.reactivex.Scheduler;
import io.reactivex.SingleTransformer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

/**
 * @author : wealive
 * @date : 2020/5/28 19:53
 * @desc :
 */
public class BkSchedulerProvider implements BaseSchedulerProvider {
    private LoginOperate operate;
    @Nullable
    private static BkSchedulerProvider INSTANCE;

    // Prevent direct instantiation.
    private BkSchedulerProvider(Context context) {
        operate = new LoginOperate(context);
    }

    public static synchronized BkSchedulerProvider getInstance(Context context) {
        if (INSTANCE == null) {
            INSTANCE = new BkSchedulerProvider(context);
        }
        return INSTANCE;
    }

    @Override
    @NonNull
    public Scheduler computation() {
        return Schedulers.computation();
    }

    @Override
    @NonNull
    public Scheduler io() {
        return Schedulers.io();
    }

    @Override
    @NonNull
    public Scheduler ui() {
        return AndroidSchedulers.mainThread();
    }

    @Override
    public Scheduler newThread() {
        return Schedulers.newThread();
    }

    @NonNull
    @Override
    public <T> SingleTransformer<T, T> applySchedulers() {
        return observable -> observable.doOnSubscribe(disposable -> {
//            if (operate.tokenCheck(JCPT_BASE_URL) != 1)
//                operate.reLoginReWrite();
        }).subscribeOn(io())
                .observeOn(ui());
    }

    @Override
    public <T> ObservableTransformer<T, T> schedulers() {
        return observable -> observable.doOnSubscribe(disposable -> {
//            if (operate.tokenCheck(JCPT_BASE_URL) != 1)
//                operate.reLoginReWrite();
        }).subscribeOn(io())
                .observeOn(ui());
    }
}
